/*
 * Singleton Tracker class:
 * Keeps track of different marks.
 */

#ifndef TRACKER_H_
#define TRACKER_H_



#include <iostream>
#include <stdio.h>
#include <string.h>
#include <list>
#include <highgui.h>
#include <cxcore.h>
#include <cv.h>
#include "bloblib/BlobResult.h"
#include "mark.h"
#include "tracking_settings.h"



using namespace cv;
using namespace std;

class CTracker {

public:
	//! Singleton object access function
	static CTracker* instance();
	//! extract all relevant tracking informations from a picture. returns true if at least one mark has been found
	bool processFrame(Mat &mat);
	//! if a mark has been found and starts being tracked, this function returns a picture of it
	Mat* getTrackedMark();
	//! returns the latest processed frame containing all informations
	Mat& latestProcessedFrame();

	//! write a string into a frame (top left)
	static void writeStringToFrame(Mat& frame, string str );

private:
	//! CTOR
	CTracker();
	//! DTOR
	~CTracker();
	//! try to merge the new marks with the ones already known
	void processMarks();
	//! increments the updateCounters of all tracked marks
	void incAllUpdateCounters() const;
	//! performs a blob analysis on the overloaded mat. The results are written to the unprocessedMarks list
	void blobAnalysis(cv::Mat &mat);
	//! filter the crap out of the the unprocessed marks
	void filterUnprocessedMarks();
	//! draws the data of the tracked marks into the overloaded mat
	void drawDataMat(cv::Mat &mat) const;
	//! draws the data of the unprocessed marks into the overloaded mat
	void drawDebugDataMat(cv::Mat &mat) const;
	//! draw some useful informations into the overloaded mat
	void drawHUD(cv::Mat &mat) const;
	//! applys the floormask to picture. All irrelevant areas are blacked out
	void applyFloorMask(Mat &mat);
	//! creates a picture of a tracked mark and adds it to the result list
	void createResultPic(CMark* mark);

	//! single instance of the tracker
	static CTracker *m_instance;
	//! list of not-yet processed marks
	list<CMark*> m_unprocessedMarks;
	//! marks that are currently being tracked
	list<CMark*> m_trackedMarks;
	//! number of marks that have been tracked
	int m_nOfMarks;
	//! contains one picture of each mark that has been tracked
	list<Mat*> m_results;
	//! current frame
	Mat m_currentFrame;
	//! latest frame, including all informations
	Mat m_latestFrame;


	//! tmp blobimage
	IplImage *m_blobImg;
};

#endif /* TRACKER_H_ */
